package com.fairytask.ui.effect
{
	import com.fairytask.engine.GameStage;
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	import com.greensock.easing.Cubic;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;

	/**
	 * 开启功能按钮效果
	 * @author xujh
	 * 
	 */
	public class TopBtnEffect extends Sprite
	{
		private var _mc		:DisplayObject;	//效果对象
		private var _postArr	:Array	= [];	//位置数据
		private var itemSpace	:Sprite;
		private var func		:Function;
		private var _arr		:Array;		//位置参数
		
		//开始位置
		private var _startX	:int = 450;
		private var _startY	:int = 280;
		
		public function TopBtnEffect(mc:DisplayObject, targetX:int=0, targetY:int=0, onComplete:Function=null,arr:Array=null)
		{
			if(mc == null)
			{
				return;
			}
			_postArr = [targetX,targetY];
			_mc = mc;
			func = onComplete;
			_arr = arr;
			init();
		}
		
		private function init():void
		{
			/*itemSpace = new Sprite();
			itemSpace.x = _startX;
			itemSpace.y = _startY;
			itemSpace.addChild(_mc);
			addChild(itemSpace);*/
			_mc.x = _startX;
			_mc.y = _startY;
			_mc.scaleX = 0.5;
			_mc.scaleY = 0.5;
			_mc.alpha = 0;
			addChild(_mc);
			TweenLite.to(_mc,0.5,{alpha:1,scaleX:1,scaleY:1,onComplete:onFilter});
		}
		
		/**
		 * 闪光一下 
		 * 
		 */
		private function onFilter():void
		{
			TweenMax.to(_mc, 0.5, {glowFilter:{color:0xFFFFFF, alpha:1, blurX:40, blurY:40, strength:2, quality:1},repeat:1,yoyo:true,startAt:{glowFilter:{color:0xFFFFFF, alpha:1, blurX:0, blurY:0, strength:1, quality:1}},onComplete:onMove});
		}
		
		/**
		 * 播放运动动画 
		 * 
		 */
		private function onMove():void
		{
			_mc.filters = null;
			//弧线锚定位置				终点位置			
			TweenLite.to(_mc,3,{x:_postArr[0], y:_postArr[1],ease:Cubic.easeOut,onComplete:onComplete});			
		}
		
		private function onComplete():void
		{
			if(null != func)
			{
				if(null != _arr)
				{
					func(_mc,_arr);
				}
				else
				{
					func(_mc);
				}
			}
			hide();
		}
		
		public function show():void
		{
			GameStage.uiSpace.addTop(this);
		}
		
		public function hide():void
		{
			if(null != this.parent)
			{
				this.parent.removeChild(this);
			}
		}
	}
}